//
//  CTEmojiCollectionView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/12.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTEmojiCollectionView.h"
#import "CTHeader.h"
#import "CTEmojiCell.h"
#import "CTDebugManager.h"
#import "CTPublishItems.h"

@interface CTEmojiCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionView *userFeaturesCollectionView;
    CGSize size;
    NSArray *emojiArray;
    NSArray *emojiBoboleItemArray;
    BOOL  isFirstReload;
    NSIndexPath * currentIndexPath;
}

@end

@implementation CTEmojiCollectionView
@synthesize delegate;
- (void)dealloc
{
    currentIndexPath = nil;
    emojiArray = nil;
    delegate = nil;
    [userFeaturesCollectionView removeFromSuperview];
    userFeaturesCollectionView = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTEmojiDetailsCollection Is Removed!!!"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        isFirstReload = YES;
        
        self.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
        size = CGSizeMake(80, 74);
        [self createUserCollectionView];
    }
    return self;
}

//创建CollectionView
- (void)createUserCollectionView
{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //水平滑动
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 0);//头部
    userFeaturesCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, CT_HEADER_SCREEN_WIDTH, 75) collectionViewLayout:flowLayout];
    // homePageCollectionView.showsVerticalScrollIndicator = NO;
    userFeaturesCollectionView.delegate = self;
    userFeaturesCollectionView.dataSource = self;
//    userFeaturesCollectionView.contentSize = CGSizeMake(20*100, 75);
    [self addSubview:userFeaturesCollectionView];
    userFeaturesCollectionView.backgroundColor = [UIColor clearColor];
    [userFeaturesCollectionView registerClass:[CTEmojiCell class] forCellWithReuseIdentifier:@"cell"];
    [userFeaturesCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
}



#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return emojiArray.count+1;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"cell";
    
    CTEmojiCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
    
    
    if (indexPath.row ==0)
    {
        CTEditeBubbleListItem *item = [emojiBoboleItemArray objectAtIndex:indexPath.row];
        if (item.image == nil) {
            cell.imgUrl = item.url;
        }else{
            cell.img = item.image;
        }
    }
    else
    {
        CTEditeEmojiListItem *item = [emojiArray objectAtIndex:indexPath.row-1];
        CTEditeEmojiListDetailsItem *detailsItem = [item.list objectAtIndex:0];
        cell.imgUrl = detailsItem.thumb;

    }
   
    if (isFirstReload == YES) {
        if (indexPath.row == 0) {
            currentIndexPath = indexPath;
            cell.selected = YES;
        }
    }
    
    return cell;
}

//头部显示的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    
    //    [headerView addSubview:advertisingColumnView];//头部广告栏
    return headerView;
}



#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
    
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,0.5,0.5,0.5);
}
//定义UICollectionView 每个item的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0.5;
}

//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.5;
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (isFirstReload == YES) {
        
        if (indexPath.row != 0) {
            CTEmojiCell * cell = (CTEmojiCell *)[userFeaturesCollectionView cellForItemAtIndexPath:currentIndexPath];
            
            cell.selected = NO;  
        }
        
        isFirstReload = NO;
    }
    
    if (indexPath.row == 0)
    {
        if ([self.delegate respondsToSelector:@selector(selectedEmoji:withItems:withIndexPath:)])
        {
            [self.delegate selectedEmoji:self withItems:emojiBoboleItemArray withIndexPath:0];
        }
    }
    else
    {
        CTEditeEmojiListItem *item = [emojiArray objectAtIndex:indexPath.row-1];
        if ([self.delegate respondsToSelector:@selector(selectedEmojiSubjects:withItems:withItem:)])
        {
            [self.delegate selectedEmojiSubjects:self withItems:item.list withItem:item];
        }
    }
 
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
#pragma markd -setter-
- (void)setEmojiCollectionArray:(NSArray *)itemArray
{
    emojiArray = itemArray;
    [userFeaturesCollectionView reloadData];
    
    CTEditeEmojiListItem *item;
    if (emojiArray != nil || emojiArray !=0) {
        item = [emojiArray objectAtIndex:0];
        
//        if ([self.delegate respondsToSelector:@selector(selectedEmojiSubjects:withItems:withIndexPath:)])
//        {
//            [self.delegate selectedEmojiSubjects:self withItems:item.list withIndexPath:0];
//        }
    }
    
}
- (void)setbobblesItem:(NSArray *)bobbleItemArray
{
    emojiBoboleItemArray = bobbleItemArray;
    [userFeaturesCollectionView reloadData];
    if ([self.delegate respondsToSelector:@selector(selectedEmoji:withItems:withIndexPath:)])
    {
        [self.delegate selectedEmoji:self withItems:emojiBoboleItemArray withIndexPath:0];
    }

}

@end
